#include "src/parser/sql_parser.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Test cases for all commands mentioned in help
const char* test_sql_commands[] = {
    // Basic SQL Commands (from help)
    "CREATE TABLE test (id INT PRIMARY KEY, name VARCHAR(50));",
    "INSERT INTO test VALUES (1, 'hello');",
    "SELECT * FROM test;",
    "UPDATE test SET name = 'world' WHERE id = 1;",
    "DELETE FROM test WHERE id = 1;",
    "DROP TABLE test;",
    
    // Built-in Commands (already tested, but let's verify again)
    "SHOW DATABASES;",
    "SHOW TABLES;", 
    "SHOW STATUS;",
    "USE test_database;",
    "DESCRIBE test_table;",
    "DESC test_table;",
    "SHOW CREATE TABLE test_table;",
    "SHOW COLUMNS FROM test_table;",
    
    // Additional SQL features
    "SELECT id, name FROM test WHERE id > 1 ORDER BY name;",
    "CREATE TABLE products (id INT PRIMARY KEY, name VARCHAR(100) NOT NULL, price FLOAT DEFAULT 0.0);",
    "INSERT INTO products (id, name, price) VALUES (1, 'Apple', 1.50);",
    
    // Edge cases and complex queries
    "SELECT COUNT(*) FROM test;",
    "UPDATE test SET name = 'updated' WHERE id IN (1, 2, 3);",
    "DELETE FROM test WHERE name LIKE 'test%';",
    
    NULL
};

void test_statement_parsing() {
    printf("=== Comprehensive SQL Parser Test ===\n\n");
    
    int passed = 0;
    int failed = 0;
    
    for (int i = 0; test_sql_commands[i] != NULL; i++) {
        printf("%d. Testing: %s\n", i + 1, test_sql_commands[i]);
        
        parser_t* parser = parser_create(test_sql_commands[i]);
        if (!parser) {
            printf("   ❌ Failed to create parser\n");
            failed++;
            continue;
        }
        
        statement_t* stmt = parser_parse_statement(parser);
        if (!stmt) {
            printf("   ❌ Parse failed: %s\n", 
                   parser_get_error(parser) ? parser_get_error(parser) : "Unknown error");
            failed++;
        } else {
            printf("   ✅ Successfully parsed as: %s\n", stmt_type_to_string(stmt->type));
            
            // Print specific details for some statement types
            switch (stmt->type) {
                case STMT_CREATE_TABLE:
                    if (stmt->data.create_table.table_name) {
                        printf("      Table: %s\n", stmt->data.create_table.table_name);
                    }
                    break;
                case STMT_INSERT:
                    if (stmt->data.insert.table_name) {
                        printf("      Table: %s\n", stmt->data.insert.table_name);
                    }
                    break;
                case STMT_SELECT:
                    // SELECT details are complex, just note it parsed
                    break;
                case STMT_UPDATE:
                    if (stmt->data.update.table_name) {
                        printf("      Table: %s\n", stmt->data.update.table_name);
                    }
                    break;
                case STMT_DELETE:
                    if (stmt->data.delete.table_name) {
                        printf("      Table: %s\n", stmt->data.delete.table_name);
                    }
                    break;
                case STMT_DESCRIBE:
                    if (stmt->data.describe.table_name) {
                        printf("      Table: %s\n", stmt->data.describe.table_name);
                    }
                    break;
                case STMT_USE:
                    if (stmt->data.use.database_name) {
                        printf("      Database: %s\n", stmt->data.use.database_name);
                    }
                    break;
                default:
                    break;
            }
            
            passed++;
            statement_destroy(stmt);
        }
        
        parser_destroy(parser);
        printf("\n");
    }
    
    printf("=== Test Summary ===\n");
    printf("✅ Passed: %d\n", passed);
    printf("❌ Failed: %d\n", failed);
    printf("📊 Success Rate: %.1f%%\n", (float)passed / (passed + failed) * 100);
    
    if (failed == 0) {
        printf("\n🎉 All commands from help documentation are parsing correctly!\n");
    } else {
        printf("\n⚠️  Some commands need attention.\n");
    }
}

int main() {
    test_statement_parsing();
    return 0;
}